﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.SqlClient;
using System.Xml;

namespace OpalisPolicyTreeView
{
    public partial class TreeViewForm : Form
    {
        private string databaseServer = "OPALIS-SVR";
        private string databaseName = "OPALIS";
        private SqlConnection myConnection;

        public string DatabaseServer
        {
            get { return databaseServer; }
            set { databaseServer = value; }
        }

        public string DatabaseName
        {
            get { return databaseName; }
            set { databaseName = value; }
        }
        



        public TreeViewForm()
        {
            InitializeComponent();
            FillTreeView();
            dataGridView1.Columns.Add("PropertyName", "Property Name");
            dataGridView1.Columns.Add("PropertyValue", "Property Value");
            dataGridView1.Visible = false;
        }


        private void FillTreeView()
        {
            
            try
            {
                string SQLconnstr = "server=" + DatabaseServer + ";Integrated Security=SSPI;database=" + DatabaseName;
                myConnection = new SqlConnection(SQLconnstr);
                myConnection.Open();
                treeView1.Nodes.Clear();
                TreeNode NodeRoot = treeView1.Nodes.Add("Policies");
                NodeRoot.Tag = "00000000-0000-0000-0000-000000000000";
                FillTreeNode(NodeRoot);
                NodeRoot.Expand();
            }
            catch (Exception ex)
            {
                
            }

           
        }

        private void FillTreeNode(TreeNode Node)
        {
            try
            {
                SqlCommand myCommand;
                SqlDataReader dr;
                TreeNode NewNode;

                treeView1.Visible = true;

                //Retrieve folders
                myCommand = new SqlCommand("SELECT Name, UniqueID from FOLDERS WHERE ParentID='" + Node.Tag.ToString() + "' AND deleted = 'False' ORDER BY Name", myConnection);
                dr = myCommand.ExecuteReader();
                while (dr.Read())
                {
                    NewNode = Node.Nodes.Add(dr["Name"].ToString());
                    NewNode.Tag = dr["UniqueID"].ToString();
                }
                dr.Close();

                //Retrieve policies, listing them as blue items in the tree
                myCommand = new SqlCommand("select DISTINCT POLICIES.Name, POLICIES.UniqueID, FOLDERS.Name As PFName from POLICIES, FOLDERS where FOLDERS.UniqueID = POLICIES.ParentID AND POLICIES.Deleted IS NULL AND POLICIES.ParentID = '" + Node.Tag.ToString() + "' ORDER BY POLICIES.NAME", myConnection);
                dr = myCommand.ExecuteReader();
                while (dr.Read())
                {
                    NewNode = Node.Nodes.Add(dr["Name"].ToString());
                    NewNode.Tag = dr["UniqueID"].ToString();
                    NewNode.ForeColor = Color.Blue;
                }
                dr.Close();

                //Continue recursive search in subfolders
                foreach (TreeNode subNode in Node.Nodes)
                {
                    if (subNode.ForeColor != Color.Blue)
                    {
                        FillTreeNode(subNode);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

 

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode.Tag.ToString() != null)
            {
                dataGridView1.Visible = true;
                dataGridView1.Rows.Clear();
                using (SqlDataAdapter da = new SqlDataAdapter("select  * from POLICIES where POLICIES.UniqueID  = '" + treeView1.SelectedNode.Tag.ToString() + "'", myConnection))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    int counter = 0;

                    foreach (DataRow row in dt.Rows) // Loop over the rows.
                    {
                        foreach (var item in row.ItemArray) // Loop over the items.
                        {
                            //MessageBox.Show("counter: " + counter.ToString());
                            //MessageBox.Show("column name: " + row.Table.Columns[counter].ColumnName.ToString());
                            //MessageBox.Show("column value: " + item.ToString());

                            dataGridView1.Rows.Add();
                            dataGridView1.Rows[counter].Cells["PropertyName"].Value = row.Table.Columns[counter].ColumnName.ToString();
                            dataGridView1.Rows[counter].Cells["PropertyValue"].Value = item.ToString();
                            counter++;
                        }
                    }

                    dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

                }
            }
        }
    }
}
